<template>
  <div class="search-page">
    <!-- 搜索框区域 -->
    <div class="search-header">
      <input type="text" v-model="keyword" placeholder="请输入搜索关键词" class="search-input" @keyup.enter="handleSearch" />
      <button class="search-btn" @click="handleSearch">搜索</button>
    </div>

    <!-- 筛选区域 -->
    <div class="filter-section card">
      <div class="filter-group">
        <span class="filter-label">商品分类：</span>
        <select v-model="filterCategory" @change="handleFilter">
          <option value="">全部</option>
          <option v-for="cat in categories" :key="cat.id" :value="cat.id">{{ cat.name }}</option>
        </select>
      </div>
      <div class="filter-group">
        <span class="filter-label">价格区间：</span>
        <input type="number" v-model="priceRange.min" placeholder="最低" class="price-input" />
        <span>-</span>
        <input type="number" v-model="priceRange.max" placeholder="最高" class="price-input" />
      </div>
      <div class="filter-group">
        <span class="filter-label">品牌：</span>
        <select v-model="filterBrand" @change="handleFilter">
          <option value="">全部</option>
          <option v-for="brand in brands" :key="brand" :value="brand">{{ brand }}</option>
        </select>
      </div>
    </div>

    <!-- 结果展示 -->
    <div class="result-section">
      <div class="sort-bar">
        <span>排序：</span>
        <select v-model="sortBy" @change="handleSort">
          <option value="priceAsc">价格升序</option>
          <option value="priceDesc">价格降序</option>
          <option value="sales">销量优先</option>
          <option value="rating">评分优先</option>
        </select>
      </div>
      <div class="product-grid">
        <div v-for="product in filteredProducts" :key="product.id" class="product-card">
          <img :src="product.image" alt="商品图片" class="product-img" />
          <h3 class="product-name">{{ product.name }}</h3>
          <p class="product-price">￥{{ product.price }}</p>
          <p class="product-sales">销量：{{ product.sales }}</p>
          <p class="product-rating">评分：{{ product.rating }}★</p>
          <div class="product-actions">
            <button class="btn-view" @click="viewProductDetail(product)">查看详情</button>
            <button class="btn-add" @click="addToCart(product)">加入购物车</button>
          </div>
        </div>
      </div>
      <div class="pagination" v-if="totalPages > 1">
        <button :disabled="currentPage === 1" @click="currentPage--">上一页</button>
        <span>第{{ currentPage }}页 / 共{{ totalPages }}页</span>
        <button :disabled="currentPage === totalPages" @click="currentPage++">下一页</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();

// 搜索关键词
const keyword = ref('');

// 筛选条件
const filterCategory = ref('');
const priceRange = ref({ min: null, max: null });
const filterBrand = ref('');

// 排序条件
const sortBy = ref('priceAsc');

// 商品数据（模拟）
const allProducts = ref([
  { id: 1, name: '智能手表', price: 199, sales: 1234, rating: 4.8, image: 'https://picsum.photos/seed/watch/400/300', category: 'electronics', brand: 'TechBrand' },
  { id: 2, name: '纯棉T恤', price: 99, sales: 5678, rating: 4.5, image: 'https://picsum.photos/seed/tshirt/400/300', category: 'clothes', brand: 'FashionBrand' },
  { id: 3, name: '无线耳机', price: 299, sales: 8765, rating: 4.9, image: 'https://picsum.photos/seed/headphones/400/300', category: 'electronics', brand: 'TechBrand' },
  { id: 4, name: '运动鞋', price: 399, sales: 4321, rating: 4.7, image: 'https://picsum.photos/seed/shoes/400/300', category: 'clothes', brand: 'FashionBrand' },
  { id: 5, name: '智能音箱', price: 149, sales: 6543, rating: 4.6, image: 'https://picsum.photos/seed/speaker/400/300', category: 'electronics', brand: 'HomeBrand' },
  { id: 6, name: '床上四件套', price: 249, sales: 2345, rating: 4.8, image: 'https://picsum.photos/seed/bedsheet/400/300', category: 'home', brand: 'HomeBrand' },
  { id: 7, name: '蓝牙音箱', price: 179, sales: 5432, rating: 4.5, image: 'https://picsum.photos/seed/bt-speaker/400/300', category: 'electronics', brand: 'TechBrand' },
  { id: 8, name: '牛仔裤', price: 159, sales: 3456, rating: 4.7, image: 'https://picsum.photos/seed/jeans/400/300', category: 'clothes', brand: 'FashionBrand' },
  { id: 9, name: '台灯', price: 89, sales: 7654, rating: 4.4, image: 'https://picsum.photos/seed/light/400/300', category: 'home', brand: 'HomeBrand' },
  { id: 10, name: '运动背包', price: 129, sales: 4567, rating: 4.6, image: 'https://picsum.photos/seed/backpack/400/300', category: 'clothes', brand: 'FashionBrand' },
  { id: 11, name: '电饭煲', price: 229, sales: 3210, rating: 4.9, image: 'https://picsum.photos/seed/rice-cooker/400/300', category: 'home', brand: 'HomeBrand' },
  { id: 12, name: '平板电脑', price: 1299, sales: 1098, rating: 4.8, image: 'https://picsum.photos/seed/tablet/400/300', category: 'electronics', brand: 'TechBrand' }
]);

// 分类数据（模拟）
const categories = ref([
  { id: 'electronics', name: '电子产品' },
  { id: 'clothes', name: '服装' },
  { id: 'home', name: '家居' }
]);

// 品牌数据（模拟）
const brands = ref(['TechBrand', 'FashionBrand', 'HomeBrand']);

// 分页相关
const currentPage = ref(1);
const pageSize = ref(6);
const totalPages = computed(() => Math.ceil(filteredProducts.value.length / pageSize.value));

// 计算筛选后的数据
const filteredProducts = computed(() => {
  let res = allProducts.value.filter(prod => {
    const keywordMatch = keyword.value ? prod.name.includes(keyword.value) : true;
    const categoryMatch = !filterCategory.value || prod.category === filterCategory.value;
    const priceMatch = (!priceRange.value.min || prod.price >= priceRange.value.min) && (!priceRange.value.max || prod.price <= priceRange.value.max);
    const brandMatch = !filterBrand.value || prod.brand === filterBrand.value;
    return keywordMatch && categoryMatch && priceMatch && brandMatch;
  });

  // 排序
  switch (sortBy.value) {
    case 'priceAsc': res = res.sort((a, b) => a.price - b.price); break;
    case 'priceDesc': res = res.sort((a, b) => b.price - a.price); break;
    case 'sales': res = res.sort((a, b) => b.sales - a.sales); break;
    case 'rating': res = res.sort((a, b) => b.rating - a.rating); break;
  }

  // 分页
  const start = (currentPage.value - 1) * pageSize.value;
  const end = start + pageSize.value;
  return res.slice(start, end);
});

// 搜索处理
const handleSearch = () => {
  currentPage.value = 1;
  // 实际项目中应调用API获取数据
};

// 筛选处理
const handleFilter = () => {
  currentPage.value = 1;
  // 实际项目中应调用API获取数据
};

// 排序处理
const handleSort = () => {
  currentPage.value = 1;
  // 实际项目中应调用API获取数据
};

// 查看商品详情
const viewProductDetail = (product) => {
  router.push({ path: `/product-detail/${product.id}` });
};

// 加入购物车
const addToCart = (product) => {
  console.log('加入购物车:', product);
  alert('商品已加入购物车！');
};
</script>

<style scoped>
.search-page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  background: #f5f5f5;
}

.search-header {
  display: flex;
  gap: 15px;
  margin-bottom: 30px;
}
.search-input {
  flex: 1;
  padding: 12px 20px;
  border: 1px solid #ddd;
  border-radius: 25px;
  font-size: 16px;
  outline: none;
}
.search-btn {
  background: var(--primary-color);
  color: white;
  padding: 0 30px;
  border: none;
  border-radius: 25px;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.3s;
}
.search-btn:hover {
  background: #35906c;
}

.card {
  background: white;
  border-radius: 12px;
  padding: 25px;
  margin-bottom: 25px;
  box-shadow: 0 3px 8px rgba(0,0,0,0.05);
}
.filter-section {
  display: flex;
  flex-wrap: wrap;
  gap: 25px;
}
.filter-group {
  display: flex;
  align-items: center;
  gap: 10px;
}
.filter-label {
  color: #333;
  font-size: 16px;
}
select {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
}
.price-input {
  width: 100px;
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
}

.result-section {
  margin-top: 20px;
}
.sort-bar {
  margin-bottom: 20px;
  color: #333;
}
.product-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
}
.product-card {
  background: white;
  border-radius: 8px;
  padding: 20px;
  text-align: center;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  transition: transform 0.3s;
}
.product-card:hover {
  transform: translateY(-5px);
}
.product-img {
  width: 100%;
  height: 200px;
  object-fit: cover;
  margin-bottom: 15px;
}
.product-name {
  font-size: 18px;
  margin-bottom: 10px;
  color: #333;
}
.product-price {
  font-size: 20px;
  color: #e4393c;
  margin-bottom: 10px;
}
.product-sales,
.product-rating {
  color: #666;
  font-size: 14px;
  margin-bottom: 5px;
}
.product-actions {
  display: flex;
  gap: 10px;
  margin-top: 15px;
}
.btn-view {
  flex: 1;
  background: var(--primary-color);
  color: white;
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
.btn-add {
  flex: 1;
  background: #fff;
  color: var(--primary-color);
  border: 1px solid var(--primary-color);
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
}

.pagination {
  display: flex;
  gap: 20px;
  justify-content: center;
  margin-top: 30px;
}
.pagination button {
  padding: 8px 16px;
  border: 1px solid #ddd;
  border-radius: 4px;
  background: white;
  cursor: pointer;
}
.pagination button:disabled {
  background: #f5f5f5;
  color: #999;
  cursor: not-allowed;
}

@media (max-width: 768px) {
  .search-header {
    flex-direction: column;
  }
  .filter-section {
    flex-direction: column;
  }
  .product-grid {
    grid-template-columns: 1fr;
  }
}
</style>